interface DockWindowParent {
	closeChild: (child: egret.DisplayObject) => string;
	// clearPopup: (child: egret.DisplayObject) => string;
}

// 继承这个类会自动获得 esc,rightclose,hold 等支持
class DockWindow extends eui.Component {
	setHold(): void {
		this.addEventListener('touchBegin', this.onTapBegin, this);
	}

	onTapBegin(): void {
		MainWindow.ins().hold.addHoldDrag(300, this.onHold, this);
	}

	onHold(): void {}

	getMouseHit(): egret.DisplayObject {
		const [mx, my] = MainWindow.ins().getMousePos();
		return this.$hitTest(mx, my);
	}

	setRightClose(): void {
		this.addEventListener(MainRightClick.rightDownEvent, this.onRightClose, this);
	}

	onRightClose(): void {
		this.onClose();
	}

	onEsc(): void {
		this.onClose();
	}

	isShow: boolean;

	// onShow(_visible: boolean): void {}

	async onClose(): Promise<void> {
		this.isShow = false;
		if (this.isWindowEffect()) {
			await asyncSetEffectUI(this, 'popDown', 100);
		}
		// this.onShow(false);
		const parent: DockWindowParent = this.parent as any;
		if (parent) {
			if (parent.closeChild) {
				parent.closeChild(this);
			} else {
				this.parent.removeChild(this);
			}
		}
	}

	setVisible(visible: boolean): void {
		this.visible = visible;
	}

	btnClose: egret.DisplayObject;

	protected override childrenCreated(): void {
		super.childrenCreated();
		if (this.btnClose) {
			this.btnClose.addEventListener('touchTap', this.onClose, this);
		}
	}

	isWindowEffect(): boolean {
		return false;
	}
}
